package com.bingxue.edu.teacher.service;

import com.bingxue.edu.common.enums.StudentClassEnum;
import com.bingxue.edu.framework.common.util.ValidationUtils;
import com.bingxue.edu.management.education.model.query.LessonStudentQuery;
import com.bingxue.edu.management.education.model.req.LessonSignInReq;
import com.bingxue.edu.management.education.model.resp.LessonStudentDetailResp;
import com.bingxue.edu.management.education.service.LessonService;
import com.bingxue.edu.management.education.service.LessonStudentService;
import com.bingxue.edu.teacher.model.resp.TeacherClassLessonDetailResp;
import com.bingxue.edu.teacher.model.resp.TeacherClassLessonResp;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.bingxue.edu.management.education.model.entity.table.LessonTableDef.LESSON;

@Slf4j
@Service
@RequiredArgsConstructor
public class TeacherLessonService {

    private final LessonService lessonService;
    private final LessonStudentService lessonStudentService;

    public TeacherClassLessonDetailResp getLessonDetailById(String lessonId) {
        TeacherClassLessonDetailResp teacherLessonDetail = lessonService.getMapper().selectOneWithRelationsByIdAs(lessonId, TeacherClassLessonDetailResp.class);
        ValidationUtils.throwIfNull(teacherLessonDetail, "课次不存在");
        LessonStudentQuery lessonStudentQuery = new LessonStudentQuery();
        lessonStudentQuery.setLessonId(lessonId);
        List<LessonStudentDetailResp> lessonStudentList = lessonStudentService.getLessonStudentList(lessonStudentQuery);
        List<LessonStudentDetailResp> normalStudentList = lessonStudentList.stream().filter(item -> item.getStudentClassStatus() == StudentClassEnum.NORMAL).toList();
        List<LessonStudentDetailResp> changeClassStudentList = lessonStudentList.stream().filter(item -> item.getStudentClassStatus() == StudentClassEnum.CHANGE_CLASS).toList();
        List<LessonStudentDetailResp> changeLessonStudentList = lessonStudentList.stream().filter(item -> item.getStudentClassStatus() == StudentClassEnum.CHANGE_LESSON).toList();
        List<LessonStudentDetailResp> cancelStudentList = lessonStudentList.stream().filter(item -> item.getStudentClassStatus() == StudentClassEnum.CANCEL_CLASS || item.getStudentClassStatus() == StudentClassEnum.CANCEL_LESSON).toList();
        teacherLessonDetail.setNormalStudentList(normalStudentList);
        teacherLessonDetail.setChangeClassOutStudentList(changeClassStudentList);
        teacherLessonDetail.setChangeLessonOutStudentList(changeLessonStudentList);
        teacherLessonDetail.setCancelStudentList(cancelStudentList);
        return teacherLessonDetail;
    }

    public List<TeacherClassLessonResp> getClassLessonList(String classId) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(
                        LESSON.DEFAULT_COLUMNS,
                        LessonService.weekNameColumn().as(TeacherClassLessonResp::getWeekName)
                )
                .from(LESSON)
                .where(LESSON.CLASS_ID.eq(classId))
                .orderBy(LESSON.LESSON_INDEX,true);
        return lessonService.getMapper().selectListWithRelationsByQueryAs(queryWrapper, TeacherClassLessonResp.class);
    }

    public void signIn(LessonSignInReq req) {
        lessonService.signIn(req);
    }

    public void unSignIn(LessonSignInReq req) {
        lessonService.unSignIn(req);
    }
}
